<!DOCTYPE html>
<html lang='en'>

<head>
    <meta charset='UTF-8'>
    <meta name='viewport' content='width=device-width, initial-scale=1.0'>
    <meta http-equiv='X-UA-Compatible' content='ie=edge'>
    <title>Document</title>
</head>

<body>
    <div id='app'>
        <!-- router-link 属性绑定的方式  name的值一定要加引号-->
        <router-link :to="{name:'news',params:{id:123}}">新闻</router-link>
        <router-link to="/music">音乐</router-link>
        <router-view></router-view>
    </div>
    <script src='https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.js'></script>
    <script src="../js/vue-router.js"></script>

    <script>
        // 命名路由,有时使用地地址来回跳转不太方便,更希望使用一个别名来控制路由的跳转
        const news = {
            props: ['id'],
            template: `<div>新闻组件列表--{{id}}</div>`
        }

        const music = {
            template: `<div>音乐组件列表</div>`
        }

        // 路由对象和路由规则
        const router = new VueRouter({
            routes: [{
                    path: '/',
                    redirect: '/news'
                },
                {
                    path: '/newsaosijfrowejfiojwe/:id',
                    name: 'news',
                    component: news,
                    props: true
                },
                {
                    path: '/music',
                    component: music
                }
            ]
        })

        const vm = new Vue({
            el: '#app',
            data: {

            },
            methods: {},
            // 将路由实例挂载到Vue实例
            router,
            // 侦听器可以监听到路由地址的变化
            watch: {
                '$route'(val) {
                    console.log(val);
                }
            },
            compted: {}
        })
    </script>
</body>

</html>